﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using System.Collections.Generic;
using System.Net.Mime;
using System.Security.Claims;

namespace DFGK_WMS.WebAPI.Controllers.PC.装备维修管理
{
    /// <summary>
    /// 报修申请
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "PC")]
    public class RepairApplicationController : BaseController
    {
        private readonly IRepairApplicationService repairApplicationService;
        /// <summary>
        /// 报修申请
        /// </summary>
        /// <param name="environment"></param>
        /// <param name="repairApplicationService"></param>
        public RepairApplicationController(IWebHostEnvironment environment, IRepairApplicationService repairApplicationService) : base(environment)
        {
            this.repairApplicationService = repairApplicationService;
        }
        /// <summary>
        /// 查询报修申请
        /// </summary>
        /// <param name="name">维修装备名称</param>
        /// <param name="repairUser">维修人</param>
        /// <param name="repairTime">维修时间</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllRepairApplication(string? name, string? repairUser, DateTime? repairTime, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = repairApplicationService.GetRepairApplication(name, pageIndex, pageSize, out var totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 装备维修明细导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult RepairApplicationDown()
        {
            var list = repairApplicationService.GetRepairApplicationMsgDown();
            if (list != null)
                return RepairApplicationDown(list);
            else
                return Ok();
        }

        /// <summary>
        /// 根据装备id查询报修申请
        /// </summary>
        /// <param name="id">装备id</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRepairApplicationById(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = repairApplicationService.GetRepairApplicationById(id);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 添加报修申请
        /// </summary>
        /// <param name="repairApplication">报修申请实体类</param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRepairApplication(RepairApplication repairApplication)
        {
            repairApplication.CreateUserId = base.BaseGetNowUserId();
            repairApplication.CreateUserName = base.BaseGetNowUserName();

            var res = repairApplicationService.AddRepairApplication(repairApplication);
            if (res)
            {
                return Ok(BaseResult.Result(true, "添加成功", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败", "500"));
        }
        /// <summary>
        /// 修改报修申请管理
        /// </summary>
        /// <param name="RepairApplication">报修申请管理实体类</param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdRepairApplication(RepairApplication RepairApplication)
        {
            if (RepairApplication.Id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = repairApplicationService.UpdRepairApplication(RepairApplication);
            if (res)
            {          
                return Ok(BaseResult.Result(true, "修改成功", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败", "500"));
        }
        /// <summary>
        /// 删除报修申请管理
        /// </summary>
        /// <param name="id">报修申请管理id</param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelRepairApplication(long id)
        {
            if (id <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var res = repairApplicationService.DelRepairApplication(id);
            if (res)
            {         
                return Ok(BaseResult.Result(true, "删除成功", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败", "500"));
        }

        /// <summary>
        /// 查询报修工单信息
        /// </summary>
        /// <param name="repairTime">报修时间</param>
        /// <param name="repUser">报修人</param>
        /// <param name="repUnit">报修单位</param>
        /// <param name="repairUser">维修人</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页条数</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllRepairWorkorder(DateTime? repairTime, string? repUser, string? repUnit, string? repairUser, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                BaseResult.Result("400", "分页条件有误！");
            }
            var list = repairApplicationService.GetRepairApplication("", pageIndex, pageSize, out var totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

    }
}
